﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace WebGetter
{
    public class RuleFactory
    {
        static Dictionary<string, IWebGetRule> dicRules = new Dictionary<string, IWebGetRule>();

        static RuleFactory()
        {
            dicRules.Add("", new BaiduRule());
            dicRules.Add("baidu.com", new BaiduRule());
            dicRules.Add("tianya.cn", new TianYaRule());
        }

        private static Regex regex = new Regex(@"http[s]?://(\w+\.)*(?<root>\w+(\.com\.cn|\.net\.cn|\.org\.cn|\.gov\.cn|\.com|\.net|\.cn|\.org|\.cc|\.me|\.tel|\.mobi|\.asia|\.biz|\.info|\.name|\.tv|\.hk))", RegexOptions.Compiled);

        public static IWebGetRule GetRule(string url)
        {
            IWebGetRule rule = null;
            Match match = regex.Match(url);
            if (match.Success)
            {
                string domain = match.Groups["root"].Value;
                if (dicRules.ContainsKey(domain))
                {
                    rule = dicRules[domain];
                }
            }
            return rule ?? dicRules[""];
        }
    }
}
